# -*- coding: utf-8 -*-

import os
import sys
import datetime
import pytz
import jinja2
from openerp.osv import osv

if hasattr(sys, 'frozen'):
    # When running on compiled windows binary, we don't have access to package
    # loader.
    path = os.path.realpath(os.path.join(
        os.path.dirname(__file__), '..', 'views'))
    loader = jinja2.FileSystemLoader(path)
else:
    loader = jinja2.PackageLoader('openerp.addons.dftg_lodop', "report")

env = jinja2.Environment(loader=loader, autoescape=True)


def strftime(str, fmt):
    dt = datetime.datetime.strptime(str, '%Y-%m-%d %H:%M:%S')
    dt = dt.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Asia/Shanghai'))
    return dt.strftime(fmt.encode('utf-8')).decode('utf-8')

env.filters['strftime'] = strftime

class report_product_package(osv.AbstractModel):

    """Model used to embed lodop reports u'product_package'"""
    _name = 'report.report_product_package'
    _template = None
    _wrapped_report_class = None

    def render_html(self, cr, uid, ids, data=None, context=None):
        model = 'product.package'
        report_obj = self.pool[model]
        package_row = report_obj.browse(cr, uid, ids, context=context)
        package_row.write({'is_print': True})
        curUser = self.pool.get('res.users').browse(cr, uid, uid, context=None)
        template = env.get_template("product_package_print.html")
        return template.render({
            'package_row': package_row
            })

